#ifndef _DANIEL_KERR_GAME_H
#define _DANIEL_KERR_GAME_H

#include "driver.h"
#include "gltimer.h"
#include "gllight.h"
#include "glmaterial.h"
#include "glgraphoptions.h"
#include "glpopup.h"
#include "gltext.h"
#include "glturn.h"
#include "universe.h"

#include "Force.h"
#include "transitMgr.h"

#define MSG_SEC_OUT 5.0
#define MSG_SEC_TRAN 0.5

#define OPT_SEC_OUT 5.0
#define OPT_SEC_TRAN 0.5

#define PLANETS_ON_SCREEN 4

#define BG_RED 0.5
#define BG_GREEN 0.5
#define BG_BLUE 0.5

#define FG_RED 0.1
#define FG_GREEN 0.1
#define FG_BLUE 0.1

#define HIGH_RED 0.3
#define HIGH_GREEN 0.3
#define HIGH_BLUE 0.3

class game
{
	public:
		GAMESTATE start(GAMETYPE gtype,graphOptions &vals);
		GAMESTATE results();

	private:
		// Variables
			turn_C me;
			light_C myLight;
			material_C myMat;
			universe_C god;
			graphOptions myOpts;

			GLUquadricObj *cyl;
			GLUquadricObj *sphere;

			// Logic Information
			GAMETYPE gametype;
			bool gameOver;
			int winner;
			transitMgr boss;/*note*/

			// Text Information
			vector <string> tl_text;

			// Option Information
			int optIndex;
			bool optHidden;
			GLfloat O_BL_X_OFF;
			GLfloat O_BL_X_INCR;
			int o_r_steps,o_o_steps,o_l_steps;

			// Message Information
			bool msgHidden;
			GLfloat M_BL_X_OFF;
			GLfloat M_BL_X_INCR;
			int m_r_steps,m_o_steps,m_l_steps;
			double planetNameAlpha;
		//Functions
			void initialize();
			void display(bool withHUD=true);

			void drawPlanets();
			void drawEdges();
			void drawStars();

			void drawHUD();
			
			void showMessages();
			void hideMessages();
			void showOptions();
			void hideOptions();
			
			void displayConnect();
			void displayPlans();
			void displaySpy();
			bool displayTroops(int &numtroops);
			bool displayQuit();

			void pause();

			void set2D_env();
			void kill2D_env();

			void updateTL_Text();
};

/*------------------------------------------------------------------*/

GAMESTATE game::start(GAMETYPE gtype,graphOptions &vals)
{
	gametype = gtype;
	gameOver = false;
	winner = -1;

	myOpts = vals;
	god.initialize(myOpts.starcount);
	initialize();

	Timer fps;
	SDL_Event event;

	while(!gameOver)
	{
		// Start Timer
		fps.start();

		// Event Polling
		while(SDL_PollEvent(&event))
		{
			if(event.type==SDL_KEYDOWN)
			{
				if(!me.moving())
				{
					switch(event.key.keysym.sym)
					{
						case SDLK_LEFT:		me.lookLeft(god,myOpts.fps);	planetNameAlpha = 1.0; break;
						case SDLK_RIGHT:	me.lookRight(god,myOpts.fps);	planetNameAlpha = 1.0; break;
						
						case SDLK_RETURN:
							if(optHidden)
							{showOptions();}
							else
							{
								int numtroops=0;
								switch(optIndex)
								{
									case 0:
										if(me.ship()||(god.powner(me.LookAt())!=NEUTRAL))
										{
											if(displayTroops(numtroops))
											{
												if(numtroops > god.myUniverse()->getForce(me.At())) numtroops = god.myUniverse()->getForce(me.At());
													if(numtroops)
													{boss.add(me.At(),me.LookAt(),god.weight(me.At(),me.LookAt()),god.myUniverse()->getPlanet(me.At()).getForce().split(numtroops));}
												me.movetoPlanet(god,me.LookAt(),myOpts.fps);
											}
										}
										else
										{displayInvalidMove();}
										break;
									case 1:
										god.myUniverse()->getPlanet(me.At()).train();
										me.switchTurn(god,boss,myOpts.fps);
										optIndex=3;
										planetNameAlpha = 1.0;
										break;
									case 2:
										displaySpy();
										me.switchTurn(god,boss,myOpts.fps);
										optIndex=3;
										planetNameAlpha = 1.0;
										break;
									case 3:
										me.switchTurn(god,boss,myOpts.fps);
										optIndex=3;
										planetNameAlpha = 1.0;
										break;
									default: break;
								}
								hideOptions();
							}
							break;
						case SDLK_w: me.switchTurn(god,boss,false); break;
					}
				}

				switch(event.key.keysym.sym)
				{
					case SDLK_UP:
						showOptions();
						if(--optIndex<0){optIndex+=4;}
						break;
					case SDLK_DOWN:
						showOptions();
						if(++optIndex>3){optIndex-=4;}
						break;

					case SDLK_c: displayConnect(); break;
					case SDLK_p: displayPlans(); break;

					case SDLK_m:
						if(msgHidden)	{showMessages();}
						else			{hideMessages();}
						break;

					case SDLK_q:
					case SDLK_ESCAPE: gameOver=displayQuit(); break;
				}
			}
			else if(event.type==SDL_QUIT)
			{gameOver=displayQuit();}
		}

		// Display
		me.updateTR_Text(god);
		updateTL_Text();
	    
		display();
		
		SDL_GL_SwapBuffers();

		// Test Win/Loss Conditions
		for(int i=0;i<god.NOPLAY();++i)
		{
			if(gametype == CONQUEST)
			{
				if(god.myUniverse()->getPct(i) > 51)
				{
					gameOver = true;
					winner = i;
					break;
				}
			}
			else if(gametype == DOMINATION)
			{
				if(god.myUniverse()->getPct(i) == 100)
				{
					gameOver = true;
					winner = i;
					break;
				}
			}
		}

		// Cap Frame Rate
		while(fps.get_ticks()<1000/myOpts.fps){}
	}

	return RESULTS;
}

/*------------------------------------------------------------------*/

void game::initialize()
{
	// Set Matrix Mode
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60,(GLdouble)X_DIM/(GLdouble)Y_DIM,0.1,100.0);

	// Set Open GL Functions
	glEnable(GL_NORMALIZE);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_DEPTH);
	if(myOpts.aliasing){glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);}

	// Set Light Variables
	myLight.enable();

	//ADDED 4.29
	planetNameAlpha = 1.0;
	// Set Fog
	GLfloat fogColor[4]={0.0,0.0,0.0,1.0};
	glFogi(GL_FOG_MODE,GL_EXP2);
	glFogfv(GL_FOG_COLOR,fogColor);
	glFogf(GL_FOG_DENSITY,0.08f);
	glHint(GL_FOG_HINT,GL_DONT_CARE);
	glFogf(GL_FOG_START,10.0f);
	glFogf(GL_FOG_END,50.0f);
	//glEnable(GL_FOG);

	// Clear Color
	glClearColor(0.0,0.0,0.0,0.0);

	// Set Planet Variables
	sphere = gluNewQuadric();;	
	gluQuadricNormals(sphere,GLU_SMOOTH);
	gluQuadricTexture(sphere,GL_TRUE);

	// Set Edge Variables
	cyl = gluNewQuadric();
	gluQuadricDrawStyle(cyl,GLU_FILL);
	gluQuadricTexture(cyl,GL_TRUE);

	// Set HUD Text
	updateTL_Text();

	// Clear Message Buffer
	myMSG.clear();

	// Variables Setting
	optIndex=0;
	optHidden=true;
	O_BL_X_OFF=-140.0;
	O_BL_X_INCR=140.0/((GLfloat)myOpts.fps*OPT_SEC_TRAN);
	o_r_steps=o_o_steps=o_l_steps=0;

	msgHidden=true;
	M_BL_X_OFF=-800.0;
	M_BL_X_INCR=800.0/((GLfloat)myOpts.fps*MSG_SEC_TRAN);
	m_r_steps=m_o_steps=m_l_steps=0;

	// Texturing
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	for(int i=0;i<god.NOP();++i)
	{
		glGenTextures(1,god.ptnp(i));
		glBindTexture(GL_TEXTURE_2D,god.ptnum(i));
		SDL_Surface* bmp = IMG_Load(god.ptext(i).c_str());
		if(bmp!=NULL)
		{
			int pind = god.ptext(i).find('.');
			int size = god.ptext(i).size();
			string sub = god.ptext(i).substr(pind+1,size-(pind+1));

			if(sub=="bmp")
			{gluBuild2DMipmaps(GL_TEXTURE_2D,3,bmp->w,bmp->h,GL_BGR_EXT,GL_UNSIGNED_BYTE,bmp->pixels);}
			else if(sub=="jpg")
			{gluBuild2DMipmaps(GL_TEXTURE_2D,3,bmp->w,bmp->h,GL_RGB,GL_UNSIGNED_BYTE,bmp->pixels);}
			else
			{cout<<"Invalid Surface Type"<<endl;}
		}
		else
		{cout<<"Surface Loading Error"<<endl;}
		SDL_FreeSurface(bmp);
	}

	//additional texture loading here
	glBindTexture(GL_TEXTURE_2D,god.NOP());
	SDL_Surface* bmp = IMG_Load("images/steel.jpg");
	gluBuild2DMipmaps(GL_TEXTURE_2D,3,bmp->w,bmp->h,GL_RGB,GL_UNSIGNED_BYTE,bmp->pixels);
	SDL_FreeSurface(bmp);

	// Turn Init
	me.initialize(god,myOpts.fps);	
}

void game::display(bool withHUD)
{
	if(me.update(god,boss,myOpts.fps))
	{optIndex=3;}
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	myLight.apply();
	drawPlanets();
	drawEdges();
	drawStars();
	if(withHUD)
	{drawHUD();}
}

/*------------------------------------------------------------------*/

void game::drawPlanets()
{
	GLint owner;
	for(int i=0;i<god.NOP();++i)
	{
		if(me.At()!=i)
		{
			glPushMatrix();
				glTranslatef(god.px(i),god.py(i),god.pz(i));
				glRotatef(god.pr(i),1,1,1);
					owner = god.powner(i);
					if(owner != NEUTRAL)//if(owner == me.getInd())
					{
						// Owner Color Details						
						myMat.applyRGB(pCol[owner][0],pCol[owner][1],pCol[owner][2],0.2);
						glDisable(GL_DEPTH_TEST);
						myMat.enableBlend();
							glutSolidSphere(god.ps(i)+0.3,40,40);
						myMat.disableBlend();
						glEnable(GL_DEPTH_TEST);
					}

					// Planet Rendering
					myMat.applyTexture();
					glEnable(GL_TEXTURE_2D);
						glBindTexture(GL_TEXTURE_2D,god.ptnum(i));						
						gluSphere(sphere,god.ps(i), 40, 40);
					glDisable(GL_TEXTURE_2D);
			glPopMatrix();
		}
	}
}

void game::drawEdges()
{
	myMat.enableBlend();
	for(int j=0;j<god.NOE();++j)
	{
		if(((god.esi(j)==me.At())&&(god.eei(j)==me.LookAt()))
		||((god.eei(j)==me.At())&&(god.esi(j)==me.LookAt())))
		{
			myMat.applyEdge(god.mel(),god.ew(j),0.3);
			glPushMatrix();	
				glTranslatef(god.ex(j),god.ey(j),god.ez(j));
					glRotatef(-god.ery(j),0,1,0);
					glRotatef(-god.erx(j),1,0,0);
						gluCylinder(cyl,0.05,0.05,god.eh(j),40,1);
			glPopMatrix();
		}
	}
	myMat.disableBlend();
}

void game::drawStars()
{
	for(int i=0;i<god.NOS();++i)
	{
		myMat.applyRGB(god.sr(i),god.sg(i),god.sb(i),1.0);
		glPushMatrix();
			glTranslatef(god.sx(i),god.sy(i),god.sz(i));
				glutSolidSphere(god.ss(i),15,15);
		glPopMatrix();
	}
}

/*------------------------------------------------------------------*/

void game::drawHUD()
{
	static double PanelAlpha = 0.1;
	static int mult = 1;
	if(!msgHidden)
	{
		if(m_r_steps!=0)
		{--m_r_steps; M_BL_X_OFF+=M_BL_X_INCR;}
		else if((m_r_steps==0)&&(m_o_steps!=0))
		{--m_o_steps;}
		else if((m_r_steps==0)&&(m_o_steps==0)&&(m_l_steps!=0))
		{--m_l_steps; M_BL_X_OFF-=M_BL_X_INCR;}
		else if((m_r_steps==0)&&(m_o_steps==0)&&(m_l_steps==0))
		{msgHidden=true;}
		else
		{cout<<"ERROR"<<endl;}
	}

	if(!optHidden)
	{
		if(o_r_steps!=0)
		{--o_r_steps; O_BL_X_OFF+=O_BL_X_INCR;}
		else if((o_r_steps==0)&&(o_o_steps!=0))
		{--o_o_steps;}
		else if((o_r_steps==0)&&(o_o_steps==0)&&(o_l_steps!=0))
		{--o_l_steps; O_BL_X_OFF-=O_BL_X_INCR;}
		else if((o_r_steps==0)&&(o_o_steps==0)&&(o_l_steps==0))
		{optHidden=true;}
		else
		{cout<<"ERROR"<<endl;}
	}

	set2D_env();

		myMat.enableBlend();
			if(planetNameAlpha > 0)
			{
				planetNameAlpha -= 0.005;
				int planName = god.pname(me.LookAt()).size();
				glColor4f(0.5,0.5,0.5,planetNameAlpha);
				//glColor4f(pCol[me.getInd()][0],pCol[me.getInd()][1],pCol[me.getInd()][2],planetNameAlpha);
				glLineWidth(2);
				writeText(310,400,0.15,0.15,god.pname(me.LookAt()));
				glLineWidth(1);
			}
			if(me.ship())
			{
				glColor4f(0.45,0.45,0.45,0.5);
				glLineWidth(4.0);
				
				glBegin(GL_LINES);
					glVertex2f(260,0);			glVertex2f(295,50);
					glVertex2f(540,0);			glVertex2f(505,50);
					glVertex2f(260,600);		glVertex2f(295,550);
					glVertex2f(540,600);		glVertex2f(505,550);

					glVertex2f(0,150);			glVertex2f(150,195);
					glVertex2f(0,450);			glVertex2f(150,405);
					glVertex2f(800,150);		glVertex2f(650,195);
					glVertex2f(800,450);		glVertex2f(650,405);

					glVertex2f(295,50);			glVertex2f(505,50);
					glVertex2f(505,50);			glVertex2f(650,195);
					glVertex2f(650,195);		glVertex2f(650,405);
					glVertex2f(650,405);		glVertex2f(505,550);
					glVertex2f(295,550);		glVertex2f(505,550);
					glVertex2f(295,550);		glVertex2f(150,405);
					glVertex2f(150,195);		glVertex2f(150,405);
					glVertex2f(150,195);		glVertex2f(295,50);
				glEnd();

				if((PanelAlpha > 0.15)||(PanelAlpha < 0.0))
					mult = mult * -1;

				PanelAlpha += 0.002 * mult;

				glColor4f(pCol[me.getInd()][0]*0.5,pCol[me.getInd()][1]*0.5,pCol[me.getInd()][2]*0.5,PanelAlpha);
			
				glBegin(GL_POLYGON);
					glVertex2f(0,0);
					glVertex2f(262,0);
					glVertex2f(295,50);
					glVertex2f(150,195);
					glVertex2f(0,150);
					glVertex2f(0,0);
				glEnd();

				glBegin(GL_POLYGON);
					glVertex2f(800,0);
					glVertex2f(540,0);			glVertex2f(505,50);
					glVertex2f(650,195);		glVertex2f(800,150);
					glVertex2f(800,0);
				glEnd();
					
				glBegin(GL_POLYGON);
					glVertex2f(260,600);		glVertex2f(295,550);
					glVertex2f(505,550);		glVertex2f(540,600);
					glVertex2f(260,600);
				glEnd();				

				glBegin(GL_POLYGON);
					glVertex2f(0,600);
					glVertex2f(0,450);			glVertex2f(150,405);
					glVertex2f(295,550);		glVertex2f(260,600);
					glVertex2f(0,600);
				glEnd();
			
				glBegin(GL_POLYGON);
					glVertex2f(800,600);
					glVertex2f(800,450);		glVertex2f(650,405);
					glVertex2f(505,550);		glVertex2f(540,600);
					glVertex2f(800,600);
				glEnd();


				glBegin(GL_POLYGON);
					glVertex2f(800,450);		glVertex2f(650,405);
					glVertex2f(650,195);		glVertex2f(800,150);
				glEnd();

				glBegin(GL_POLYGON);
					glVertex2f(150,195);
					glVertex2f(0,150);
					glVertex2f(0,450);			glVertex2f(150,405);

				glEnd();

				glBegin(GL_POLYGON);
					glVertex2f(260,0);			glVertex2f(295,50);
					glVertex2f(505,50);			glVertex2f(540,0);
				glEnd();

				glLineWidth(1.0);
			}
		myMat.disableBlend();

		// Background Coloring
		glColor4f(BG_RED,BG_GREEN,BG_BLUE,1.0);
			// Top Left Text Background
			if(god.NOPLAY() < 6)	{glRectf(0,0,521,25);}
			else					{glRectf(0,0,521,45);}
			// Top Right Text Background				
			glRectf(525,0,800,102);
			// Bottom Left Messages Text Background
			glRectf(0+M_BL_X_OFF,400,800+M_BL_X_OFF,600);
			// Bottom Left Options Background
			glRectf(0+O_BL_X_OFF,524,140+O_BL_X_OFF,600);

		glColor4f(FG_RED,FG_GREEN,FG_BLUE,1.0);
			// Top Left Text Background
			if(god.NOPLAY() < 6)	{glRectf(2,2,519,23);}
			else					{glRectf(2,2,519,43);}
			// Top Right Text Background				
			glRectf(527,2,798,100);
			// Bottom Left Messages Text Background
			glRectf(2+M_BL_X_OFF,402,798+M_BL_X_OFF,598);
			// Bottom Left Options Background
			glRectf(2+O_BL_X_OFF,526,138+O_BL_X_OFF,598);

		// Selection Box
		glColor4f(0.15+pCol[me.getInd()][0]/4.0,0.15+pCol[me.getInd()][1]/4.0,0.15+pCol[me.getInd()][2]/4.0,0);
		glRectf(2+O_BL_X_OFF,526+(18*optIndex),138+O_BL_X_OFF,544+(18*optIndex));

		// Lettering
		glColor4f(0.65+pCol[me.getInd()][0]/4,0.65+pCol[me.getInd()][1]/4,0.65+pCol[me.getInd()][2]/4,0.4);
		writeText(5,			0,		0.12,	0.1,	20,	tl_text);
		writeText(530,			0,		0.12,	0.1,	16,	me.TR_Text());
		writeText(5+M_BL_X_OFF,	400,	0.16,	0.1,	18,	myMSG.msgs());
		writeText(5+O_BL_X_OFF,	521,	0.16,	0.1,	18,	me.OPT_Text());
	kill2D_env();
}

/*------------------------------------------------------------------*/

void game::showMessages()
{
	if(optHidden)
	{
		if(msgHidden)
		{
			m_r_steps=myOpts.fps*MSG_SEC_TRAN;
			m_o_steps=myOpts.fps*MSG_SEC_OUT;
			m_l_steps=myOpts.fps*MSG_SEC_TRAN;
			msgHidden=false;
		}
		else
		{
			if((m_r_steps==0)&&(m_o_steps==0)&&(m_l_steps==0))
			{
				m_r_steps=myOpts.fps*MSG_SEC_TRAN;
				m_o_steps=myOpts.fps*MSG_SEC_OUT;
				m_l_steps=myOpts.fps*MSG_SEC_TRAN;
			}
			else if((m_r_steps==0)&&(m_o_steps==0)&&(m_l_steps!=0))
			{
				m_r_steps=(myOpts.fps*MSG_SEC_TRAN)-m_l_steps;
				m_o_steps=myOpts.fps*MSG_SEC_OUT;
				m_l_steps=myOpts.fps*MSG_SEC_TRAN;
			}
			else if((m_r_steps==0)&&(m_o_steps!=0)&&(m_l_steps!=0))
			{
				m_o_steps=myOpts.fps*MSG_SEC_OUT;
				m_l_steps=myOpts.fps*MSG_SEC_TRAN;
			}
		}
	}
	else
	{hideOptions();}
}

void game::hideMessages()
{
	if(!msgHidden)
	{
		if((m_r_steps==0)&&(m_o_steps!=0)&&(m_l_steps!=0))
		{m_o_steps = 0;}
		else if((m_r_steps!=0)&&(m_o_steps!=0)&&(m_l_steps!=0))
		{
			m_l_steps = ((myOpts.fps*OPT_SEC_TRAN) - m_r_steps);
			m_r_steps = m_o_steps = 0;
		}
	}
}

void game::showOptions()
{
	if(msgHidden)
	{
		if(optHidden)
		{
			o_r_steps=myOpts.fps*OPT_SEC_TRAN;
			o_o_steps=myOpts.fps*OPT_SEC_OUT;
			o_l_steps=myOpts.fps*OPT_SEC_TRAN;
			optHidden=false;
		}
		else
		{
			if((o_r_steps==0)&&(o_o_steps==0)&&(o_l_steps==0))
			{
				o_r_steps=myOpts.fps*OPT_SEC_TRAN;
				o_o_steps=myOpts.fps*OPT_SEC_OUT;
				o_l_steps=myOpts.fps*OPT_SEC_TRAN;
			}
			else if((o_r_steps==0)&&(o_o_steps==0)&&(o_l_steps!=0))
			{
				o_r_steps=(myOpts.fps*OPT_SEC_TRAN)-o_l_steps;
				o_o_steps=myOpts.fps*OPT_SEC_OUT;
				o_l_steps=myOpts.fps*OPT_SEC_TRAN;
			}
			else if((o_r_steps==0)&&(o_o_steps!=0)&&(o_l_steps!=0))
			{
				o_o_steps=myOpts.fps*OPT_SEC_OUT;
				o_l_steps=myOpts.fps*OPT_SEC_TRAN;
			}
		}
	}
	else
	{hideMessages();}
}

void game::hideOptions()
{
	if(!optHidden)
	{
		if((o_r_steps==0)&&(o_o_steps!=0)&&(o_l_steps!=0))
		{o_o_steps = 0;}
		else if((o_r_steps!=0)&&(o_o_steps!=0)&&(o_l_steps!=0))
		{
			o_l_steps = ((myOpts.fps*OPT_SEC_TRAN) - o_r_steps);
			o_r_steps = o_o_steps = 0;
		}
	}
}

/*------------------------------------------------------------------*/

void game::displayConnect()
{
	vector <string> temp;
	temp.push_back("Connectedness Screen");

	display(false);
	set2D_env();
		// Background Coloring
		glColor4f(BG_RED,BG_GREEN,BG_BLUE,1.0);	glRectf(25,25,775,575);
		glColor4f(FG_RED,FG_GREEN,FG_BLUE,1.0);	glRectf(30,30,770,570);
		
		// Lettering
		glColor4f(1.0,1.0,1.0,0.4);
		writeText(40,40,0.15,0.15,20,temp);

		// Draw Edges
		glLineWidth(2.0);
		for(int k=0;k<god.NOE();++k)
		{
			GLfloat x1 = (sin(god.ua()*god.esi(k))*220.0) + (X_DIM/2.0);
			GLfloat y1 = (cos(god.ua()*god.esi(k))*220.0) + (Y_DIM/2.0);
			GLfloat x2 = (sin(god.ua()*god.eei(k))*220.0) + (X_DIM/2.0);
			GLfloat y2 = (cos(god.ua()*god.eei(k))*220.0) + (Y_DIM/2.0);

			glColor4f(god.ew(k)/god.mel(),0.0,(god.mel()-god.ew(k))/god.mel(),1.0);
			glBegin(GL_LINES);
				glVertex2f(x1,y1);
				glVertex2f(x2,y2);
			glEnd();
		}
		glLineWidth(1.0);

		// Draw Planets
		for(int i=0;i<god.NOP();++i)
		{				
			// Planet Shape
			int owner = god.powner(i);
			if(owner == NEUTRAL)
				glColor4f(0.0,0.0,0.0,1.0);
			else
				glColor4f(pCol[owner][0],pCol[owner][1],pCol[owner][2],1.0);

			glBegin(GL_TRIANGLE_FAN);
				for(int j=0;j<360.0;j+=10.0)
				{
					GLfloat x = (cos((j*PI)/180.0)*20.0) + (sin(god.ua()*i)*220.0) + (X_DIM/2.0);
					GLfloat y = (sin((j*PI)/180.0)*20.0) + (cos(god.ua()*i)*220.0) + (Y_DIM/2.0);
					glVertex2f(x,y);
				}
			glEnd();

			// Planet Text
			glColor4f(1.0,1.0,1.0,0.4);
			temp.clear();
			temp.push_back(god.pname(i));
			GLfloat tx = (sin(god.ua()*i)*220.0) + (X_DIM/2.0);
			GLfloat ty = (cos(god.ua()*i)*220.0) + (Y_DIM/2.0) - 50.0;
			writeText(tx,ty,0.1,0.1,20,temp);
		}
		
		SDL_GL_SwapBuffers();
	kill2D_env();
	
	pause();
}

void game::displayPlans()
{
	vector <string> title;
	title.push_back("Planet Information Screen");
	
	vector < vector <string> > info;
	for(int i=0;i<god.NOP();++i)
	{
		if(me.getInd() == god.powner(i))
		{info.push_back(god.ppinfo(i));}
	}

	int offset = 0;

	display(false);
	set2D_env();
		while(true)
		{	
			glColor4f(BG_RED,BG_GREEN,BG_BLUE,1.0);	glRectf(25,25,775,575);
			glColor4f(FG_RED,FG_GREEN,FG_BLUE,1.0);	glRectf(30,30,770,570);

			glColor4f(1.0,1.0,1.0,0.4);
			writeText(40,40,0.15,0.15,20,title);

			for(int i=0;i<PLANETS_ON_SCREEN;++i)
			{
				if((unsigned int)(i+offset)<info.size())
				{
					glColor4f(1.0,1.0,1.0,0.4);
					writeText(130,80+(i*125),0.15,0.15,20,info[i+offset]);

					int owner = me.getInd();
					glColor4f(pCol[owner][0],pCol[owner][1],pCol[owner][2],1.0);
					
					glBegin(GL_TRIANGLE_FAN);
						for(int j=0;j<360.0;j+=10.0)
						{
							GLfloat x = (cos((j*PI)/180.0)*40.0) + 80.0;
							GLfloat y = (sin((j*PI)/180.0)*40.0) + (i*125.0) + 135.0;
							glVertex2f(x,y);
						}
					glEnd();
				}
			}

			SDL_GL_SwapBuffers();

			SDL_Event event;
			SDL_WaitEvent(&event);
			if(event.type==SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_UP:	if(offset>0){--offset;}								break;
					case SDLK_DOWN:	if((offset+PLANETS_ON_SCREEN)<god.NOP()){++offset;}	break;
					default:		kill2D_env();										return;
				}
			}
		}
}

void game::displaySpy()
{
	vector <string> temp;
	temp = god.ppinfo(me.LookAt());

	set2D_env();
		glColor4f(BG_RED,BG_GREEN,BG_BLUE,1.0);	glRectf(255,210,545,395);
		glColor4f(FG_RED,FG_GREEN,FG_BLUE,1.0);	glRectf(260,215,540,390);
		glColor4f(1.0,1.0,1.0,0.4);	writeText(265,205,0.15,0.15,35,temp);
		SDL_GL_SwapBuffers();
	kill2D_env();
	
	pause();
}

bool game::displayTroops(int &numtroops)
{
	vector <string> temp;
	temp.push_back("How Many Troops:");
	temp.push_back("");

	set2D_env();
		SDL_Event event;
		bool loop=true;
		bool returnWhat = true;
		while(loop)
		{
			while(SDL_PollEvent(&event))
			{
				if(event.type==SDL_KEYDOWN)
				{
					if((event.key.keysym.sym>=SDLK_0)&&(event.key.keysym.sym<=SDLK_9))
					{if(temp[1].size()<16){temp[1]+=(char)event.key.keysym.sym;}}
					else if(event.key.keysym.sym==SDLK_BACKSPACE)
					{if(temp[1].size()>0){temp[1].erase(temp[1].size()-1);}}
					else if(event.key.keysym.sym==SDLK_RETURN)
					{loop=false;}
					else if(event.key.keysym.sym==SDLK_ESCAPE)
					{loop=false; returnWhat=false;}
					else if(event.key.keysym.sym==SDLK_q)
					{loop=false; returnWhat=false;}
				}
			}

			glColor4f(BG_RED,BG_GREEN,BG_BLUE,1.0);			glRectf(298,263,502,337);
			glColor4f(FG_RED,FG_GREEN,FG_BLUE,1.0);			glRectf(300,265,500,335);
			glColor4f(HIGH_RED,HIGH_GREEN,HIGH_BLUE,0.4);	glRectf(303,303,497,332);
			glColor4f(1.0,1.0,1.0,0.4);	writeText(305,255,0.15,0.15,35,temp);

			SDL_GL_SwapBuffers();
		}
	kill2D_env();

	numtroops = atoi(temp[1].c_str());

	return returnWhat;
}

bool game::displayQuit()
{
	vector <string> temp;
	temp.push_back("Do You Really Want To Quit?:");
	temp.push_back("    (Y)es or (N)o");

	set2D_env();
		glColor4f(BG_RED,BG_GREEN,BG_BLUE,1.0);	glRectf(295,278,505,332);
		glColor4f(FG_RED,FG_GREEN,FG_BLUE,1.0);	glRectf(297,280,503,330);
		glColor4f(1.0,1.0,1.0,0.4);	writeText(301,280,0.1,0.1,20,temp);
		SDL_GL_SwapBuffers();
	kill2D_env();

	SDL_Event event;
	while(true)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type==SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_y:	return true;	break;
					case SDLK_n:	return false;	break;
				}
			}
		}
	}
}

/*------------------------------------------------------------------*/

void game::pause()
{
	SDL_Event event;
	while(true)
	{
		while(SDL_PollEvent(&event))
		{if(event.type==SDL_KEYDOWN){return;}}
	}
}

/*------------------------------------------------------------------*/

void game::set2D_env()
{
	glPushAttrib(GL_ENABLE_BIT);
		glDisable(GL_DEPTH_TEST);	
		glDisable(GL_LIGHTING);
		glDisable(GL_BLEND);	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
		glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
		glLoadIdentity();
		gluOrtho2D(0,X_DIM,Y_DIM,0);
}

void game::kill2D_env()
{
	glMatrixMode(GL_MODELVIEW);
		glPopMatrix();
	glMatrixMode(GL_PROJECTION);
		glPopMatrix();
	glPopAttrib();
}

/*------------------------------------------------------------------*/

void game::updateTL_Text()
{
	tl_text.clear();
	char buf[10];
	string temp="";
	
	for(int i=0;i<god.NOPLAY();++i)
	{
		_itoa_s(i+1,buf,sizeof(int),10);
		temp+="Player";
		temp+=buf;
		temp+=": ";
		_itoa_s((int)god.myUniverse()->getPct(i),buf,sizeof(int),10);
		temp+=buf;
		temp+="% ";
		if((i+1)%5 == 0)
		{
			tl_text.push_back(temp);
			temp = "";
		}
	}
	tl_text.push_back(temp);
}

/*------------------------------------------------------------------*/

GAMESTATE game::results()
{
	glClearColor(0.0,0.0,0.0,0.0);
	set2D_env();	

	textBox* t = new textBox(125,510,120," Return to Title");
	textBox* q = new textBox(300,510,120," Quit");

	Star_C belt;
	belt.set2Dstars(2000);

	float r = 0.2,g = 0.2,b = 0.2;
	int mult = 5;

	Timer fps;
	SDL_Event event;
	while(true)
	{
		fps.start();

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		for(int i = 0; i < belt.NOS(); ++i)
		{
			if(i % 2 == 0)		{glColor4f(r, g, b, 0.9);}
			else				{glColor4f(1-r, 1-g, 1-b, 0.9);}

			glTranslatef(belt.sx(i),belt.sy(i),belt.sz(i));
			glutSolidSphere(belt.ss(i),10,10);
			glTranslatef(-belt.sx(i),-belt.sy(i),-belt.sz(i));
		}

		glLineWidth(8);
		glColor4f(0.5-r,0.5-g,0.5-b,1.0);
		writeText(0,200,2.0,2.0,"Results");
		glLineWidth(3.2);
		glColor4f(1-r,1-g,1-b,1.0);
		writeText(102,147,0.5,0.5,"Results");
		glColor4f(r,g,b,1.0);
		writeText(100,150,0.5,0.5,"Results");
		glLineWidth(1.0);
		glColor4f(1,1,1,1.0);
		writeText(125,170,0.1,0.1,"by the Mogs of War");
		writeText(0,600,0.1,0.1,"2007 Texas Tech University Computer Science");
			
		r += (0.001 * mult);
		g += (0.001 * mult);
		b += (0.001 * mult);

		if((r > 1)||(g > 1)||(b > 1)) mult = mult * -1;
		if((r < 0.2)||(g < 0.2)||(b < 0.2)) mult = mult * -1;

		t->draw();
		q->draw();
		myMat.enableBlend();
		glColor4f(1.0,1.0,1.0,0.3);
		glRectf(100,200,700,500);
		glColor4f(0.0,0.0,0.0,0.9);
		glRectf(100,180,700,200);
		myMat.disableBlend();
		glColor3f(1.0,1.0,1.0);
		writeText(100,200,0.1,0.1,"Player#        #Planets     %Universe Total Forces");
		int pforce, tforce;
		tforce = 0;
		vector <int> planetsOwned;

		for(int i=0;i<god.NOPLAY();++i)
		{
			if(i == winner)
			{
				glColor4f(1,1,1,1.0);
				writeText(30,(220+(i*30)),0.13,0.13,"Winner!");
			}
			pforce = 0;
			myMat.enableBlend();
			glColor4f(pCol[i][0]+(r/2.0),pCol[i][1]+(g/2.0),pCol[i][2]+(b/2.0),0.5);
			glRectf(100,220+(i*30)-20,700,220+(i*30)+10);
			myMat.disableBlend();
			glColor3f(0,0,0);

			//player name
			string ptext = "Player" + gitoa(i+1,3);
			glLineWidth(1.7);
			writeText(120,220+(i*30),0.12,0.12,ptext);

			//num planets and pct
			ptext = gitoa(god.myUniverse()->allOwned(i).size(),2) + "      " + gitoa(god.myUniverse()->getPct(i),3) + "%";
			writeText(255,220+(i*30),0.12,0.12,ptext);
			
			//total forces
			planetsOwned = god.myUniverse()->allOwned(i);
			for(unsigned int j = 0; j < planetsOwned.size(); j++)
				pforce += god.myUniverse()->getForce(planetsOwned[j]);
			ptext = gitoa(pforce,10);
			writeText(415,220+(i*30),0.12,0.12,ptext);
		}
		SDL_GL_SwapBuffers();

		if(t->getData() == 1)
		{
			return TITLE;
		}
		else if(q->getData() == 1)		{return QUIT;}

		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_q: return QUIT; break;
					case SDLK_RETURN:
					case SDLK_t: return TITLE; break;
				}
			}
			if(event.type == SDL_MOUSEBUTTONDOWN)
			{
				if(t->clicked(event.button.x, event.button.y))
				{t->onClick();}
				if(q->clicked(event.button.x, event.button.y))
				{q->onClick();}
			}
			if(event.type == SDL_MOUSEMOTION)
			{
				t->setHover(t->hover(event.motion.x,event.motion.y));
				q->setHover(q->hover(event.motion.x,event.motion.y));
			}
		}

		// Cap Frame Rate at 1 per second..
		SDL_Delay(100);
	}

}

/*------------------------------------------------------------------*/

#endif